package com.space.school.controller;

import com.space.school.domain.User;
import com.space.school.services.UserService;
import com.space.school.services.impl.UserServiceImpl;
import com.space.school.utils.PageBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;

@WebServlet(name = "userServlet",value = "/userServlet")
public class UserServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//   设置编码格式
        req.setCharacterEncoding("UTF-8");
//        取cookie
//            Cookie[]cookies=req.getCookies();
//        取session
        HttpSession session=req.getSession(false);
        String  name="";
        if(session!=null){
            name=(String) session.getAttribute("userName");
        }


//        获取当前页数
        String currentPage=req.getParameter("currentPage");
//        如果第一次进页面,当前页数为空或为null
        if(currentPage==null || currentPage.equals("")){
            currentPage="1";
        }
        //        进行数据查询 findAll 的方法
        //        将查询的结果集个放进页面
//       每页显示页数
        int pageSize=3;
//        取查询条件
        Map<String,String[]> map=req.getParameterMap();

        PageBean<User> pb=userService.search(Integer.parseInt(currentPage),pageSize,map);
        req.setAttribute("pb",pb);
        String userName=req.getParameter("userName");
        req.setAttribute("userName",req.getParameter("userName"));
        req.setAttribute("status",req.getParameter("status"));
        req.getRequestDispatcher("userList.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        条件搜索
//        1.
//        String username=req.getParameter("userName");
//        String status=req.getParameter("status");
//        取查询的条件

        req.setCharacterEncoding("UTF-8");
        //        获取当前页数
        String currentPage=req.getParameter("currentPage");
//        如果第一次进页面,当前页数为空或为null
        if(currentPage==null || currentPage.equals("")){
            currentPage="1";
        }
        //        进行数据查询 findAll 的方法
        //        将查询的结果集个放进页面
//       每页显示页数
        int pageSize=3;
        Map<String,String[]> map=req.getParameterMap();
        PageBean<User> pb=userService.search(Integer.parseInt(currentPage),pageSize,map);
        req.setAttribute("pb",pb);
        req.setAttribute("userName",req.getParameter("userName"));
        req.setAttribute("status",req.getParameter("status"));
        req.getRequestDispatcher("userList.jsp").forward(req,resp);


    }
}
